package com.eascs.workflow.aging.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.eascs.app.dao.PageResult;
import com.eascs.app.dao.SqlPageQuery;
import com.eascs.common.util.DateUtils;
import com.eascs.common.util.StringUtil;
import com.eascs.workflow.aging.dao.AgingDevDo;
import com.eascs.workflow.aging.entity.AgingExportEntity;
import com.eascs.workflow.aging.entity.DevLoanapply;
import com.eascs.workflow.aging.entity.NodeEntity;
import com.eascs.workflow.aging.entity.WorkFlowEntity;
import com.eascs.workflow.aging.form.AgingQueryForm;
import com.eascs.workflow.aging.service.IAgingDevService;
import com.eascs.workflow.common.vo.ModelResult;

@Service
public class AgingDevServiceImpl implements IAgingDevService {
	@Autowired
	AgingDevDo agingDevDao;

	@Override
	public PageResult<Object[]> listAgingInfo(AgingQueryForm entity) {
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, entity.getPageSize(), entity.getAgingIndex());
		StringBuffer querySql = new StringBuffer();
		querySql.append(" SELECT l.bill_code, l.operator_name, l.ltd_name, ");
		querySql.append(" (SELECT m1.overtime FROM wf_main m1 WHERE m1.refer_code = l.bill_code AND m1.process_type = 'CREDIT' LIMIT 0 , 1 ) AS CREDIT, ");
		querySql.append(" (SELECT m2.overtime FROM wf_main m2 WHERE m2.refer_code = l.bill_code AND m2.process_type = 'INAUDIT' LIMIT 0 , 1 ) AS INAUDIT, ");
		querySql.append(" (SELECT m3.overtime FROM wf_main m3 WHERE m3.refer_code = l.bill_code AND m3.process_type = 'BANK_REPORT' LIMIT 0 , 1) AS BANK_REPORT, ");
		querySql.append(" (SELECT m4.overtime FROM wf_main m4 WHERE m4.refer_code = l.bill_code AND m4.process_type = 'DEPOSIT' LIMIT 0 , 1 ) AS DEPOSIT, ");
		querySql.append("(SELECT sum(m5.overtime) FROM wf_main m5 WHERE m5.process_type IN ('CREDIT','INAUDIT','BANK_REPORT','DEPOSIT') AND m5.refer_code = l.bill_code) AS over ");
		querySql.append(" FROM o2odev_loanapply l WHERE 1=1 ");
		
		
		if (StringUtil.isNotBlank(entity.getBillCode())) {
			querySql.append(" AND l.bill_code = ? ");
			sqlPageQuery.addParam(entity.getBillCode());
		}
		
		if (StringUtil.isNotBlank(entity.getLtdName())) {
			querySql.append(" AND l.ltd_name like ? ");
			sqlPageQuery.addLikeParam(entity.getLtdName());
		}
		
		if (StringUtil.isNotBlank(entity.getOrgManager())) {
			querySql.append(" AND l.operator_name like ? ");
			sqlPageQuery.addLikeParam(entity.getOrgManager());			
		}
		
		if (StringUtil.isNotBlank(entity.getProvinceCode())) {
			querySql.append(" AND l.province_code = ? ");
			sqlPageQuery.addParam(entity.getProvinceCode());			
		}
		
		if (!StringUtil.isEmpty(entity.getCreateDateStart())) {
			querySql.append(" AND l.CREATE_DATE >= ? ");
			sqlPageQuery.addParam(entity.getCreateDateStart());
		}
		
		if (!StringUtil.isEmpty(entity.getCreateDateStart())) {
			querySql.append(" AND l.CREATE_DATE < ?");
			sqlPageQuery.addParam(DateUtils.formateStandDate(DateUtils.addDate(DateUtils.formateStandDate(entity.getCreateDateStart()), 1)));
		}
		
		if (StringUtil.isNotBlank(entity.getDebtStatus())) {
			querySql.append(" AND l.wf_type = ?");
			sqlPageQuery.addParam(entity.getDebtStatus());
		}
		
		sqlPageQuery.setQuerySql(querySql.toString());
		return agingDevDao.findDataWithPage(sqlPageQuery);
	}

	@Override
	public ModelResult<Boolean> insertLoanapply(DevLoanapply vo) {
		ModelResult<Boolean> model = new ModelResult<Boolean>();
		int insert = agingDevDao.insert(vo);
		if (insert > 0) {
			model.setModel(true);
		} 
		return model;
	}

	@Override
	public ModelResult<Boolean> insertWorkFlow(WorkFlowEntity vo) {
		ModelResult<Boolean> model = new ModelResult<Boolean>();
		int insert = agingDevDao.insert(vo);
		if (insert > 0) {
			model.setModel(true);
		} 
		return model;
	}

	@Override
	public ModelResult<Boolean> insertNode(NodeEntity vo) {
		ModelResult<Boolean> model = new ModelResult<Boolean>();
		int insert = agingDevDao.insert(vo);
		if (insert > 0) {
			model.setModel(true);
		} 
		return model;
	}

	@Override
	public PageResult<Object[]> listNodeInfo(String billCode, String nodeType) {
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, Integer.MAX_VALUE, 0);
		StringBuffer querySql = new StringBuffer();
		querySql.append(" SELECT m.used_time , m.overtime FROM wf_main m WHERE m.refer_code = ? AND m.process_type = ? ");
		sqlPageQuery.addParam(billCode);
		sqlPageQuery.addParam(nodeType);
		sqlPageQuery.setQuerySql(querySql.toString());
		return agingDevDao.findDataWithPage(sqlPageQuery);
	}

	@Override
	public ModelResult<Boolean> deleteNode() {
		// TODO Auto-generated method stub
		ModelResult<Boolean> model = new ModelResult<Boolean>();
		StringBuffer querySql = new StringBuffer();
		querySql.append(" DELETE FROM wf_node;");
		int delete =agingDevDao.execute(querySql.toString(), null);
		if(delete>0)
		{
			model.setModel(true);
		}
		return model;
	}

	@Override
	public ModelResult<Boolean> deleteWorkflow() {
		// TODO Auto-generated method stub
		ModelResult<Boolean> model = new ModelResult<Boolean>();
		StringBuffer querySql = new StringBuffer();
		querySql.append(" DELETE FROM wf_main;");
		int delete =agingDevDao.execute(querySql.toString(), null);
		if(delete>0)
		{
			model.setModel(true);
		}
		return model;
		
	}

	@Override
	public ModelResult<Boolean> deleteLoanApply() {
		ModelResult<Boolean> model = new ModelResult<Boolean>();
		StringBuffer querySql = new StringBuffer();
		querySql.append(" DELETE FROM o2odev_loanapply;");
		int delete = agingDevDao.execute(querySql.toString(), null);
		if(delete>0)
		{
			System.out.println("删除成功！");
			model.setModel(true);
		}
		else{
			
			System.out.println("删除失败！");
		}
		return model;
		
	}

	@Override
	public PageResult<Object[]> agingInfoList(AgingQueryForm entity) {
		SqlPageQuery<Object[]> sqlPageQuery = new SqlPageQuery<>(Object[].class, entity.getPageSize(), entity.getAgingIndex());
		StringBuffer querySql = new StringBuffer();
		
		querySql.append(" SELECT loanapply_no,province_name,ltd_name,operator_name,loan_type,custome_rname,");
		querySql.append(" 0 as zx_setup0, zx_setup1,zx_setup2,zx_setup3,zx_sum,");
		querySql.append(" 0 as ns_setup0,ns_setup1,ns_setup2,ns_setup3,ns_setup4,ns_setup5,ns_setup6,ns_sum,");
		querySql.append(" 0 as mqtb_setup0,mqtb_setup1,mqtb_setup2,mqtb_setup3,mqtb_setup4,mqtb_sum,");
		querySql.append(" 0 as bzj_setup0,bzj_setup1,bzj_setup2,bzj_sum,");
		querySql.append(" 0 as cwsh_setup0,cwsh_setup1,cwsh_setup1 as cwsh_sum,yhsb_sum,l.sum");
		querySql.append(" FROM aging_export  l	WHERE 1=1 	");
		
		if (StringUtil.isNotBlank(entity.getBillCode())) {
			querySql.append(" AND l.bill_code = ? ");
			sqlPageQuery.addParam(entity.getBillCode());
		}
		
		if (StringUtil.isNotBlank(entity.getLtdName())) {
			querySql.append(" AND l.ltd_name like ? ");
			sqlPageQuery.addLikeParam(entity.getLtdName());
		}
		
		if (StringUtil.isNotBlank(entity.getOrgManager())) {
			querySql.append(" AND l.operator_name like ? ");
			sqlPageQuery.addLikeParam(entity.getOrgManager());			
		}
		
		if (StringUtil.isNotBlank(entity.getProvinceCode())) {
			querySql.append(" AND l.province_code = ? ");
			sqlPageQuery.addParam(entity.getProvinceCode());			
		}
		
		if (!StringUtil.isEmpty(entity.getCreateDateStart())) {
			querySql.append(" AND l.CREATE_DATE >= ? ");
			sqlPageQuery.addParam(entity.getCreateDateStart());
		}
		
		if (!StringUtil.isEmpty(entity.getCreateDateStart())) {
			querySql.append(" AND l.CREATE_DATE < ?");
			sqlPageQuery.addParam(DateUtils.formateStandDate(DateUtils.addDate(DateUtils.formateStandDate(entity.getCreateDateStart()), 1)));
		}
		
		if (StringUtil.isNotBlank(entity.getDebtStatus())) {
			querySql.append(" AND l.wf_type = ?");
			sqlPageQuery.addParam(entity.getDebtStatus());
		}
		
		sqlPageQuery.setQuerySql(querySql.toString());
		return agingDevDao.findDataWithPage(sqlPageQuery);
	}

	@Override
	public ModelResult<Boolean> insertAgingExport(AgingExportEntity vo) {
		return agingDevDao.insertAgingExport(vo);
	}

	@Override
	public ModelResult<Boolean> deleteAgingExport() {
		return agingDevDao.deleteAgingExport();
	}
	
	
}	
